package com.lysj.bhp.financial.service;

import com.lysj.bhp.financial.base.FinancialContent;
import com.lysj.bhp.financial.domain.Financial;
import com.lysj.bhp.financial.domain.UserEarnings;
import com.lysj.bhp.financial.domain.UserFinancialRecord;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

@Service
@Transactional
public class FinancialPurchaseService extends FinancialContent {

    public Resp financialPurchase(Long financialId, BigDecimal investmentNum) {
        Long currentTime = System.currentTimeMillis();
        //校验判空处理
        if (ParamUtil.isBlack(financialId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财产品");
        }
        Financial financial = financialRepository.findOne(financialId);
        if (null == financial) {
            return new Resp(Resp.PARAM_ERROR, "请选择理财产品");
        }
        if (currentTime > financial.getInterestDate().getTime()) {
            return new Resp(Resp.PARAM_ERROR, "理财产品购买时间已结束");
        }
        if (investmentNum.compareTo(financial.getMinInvestment()) == -1) {
            return new Resp(Resp.PARAM_ERROR, "投资金额小于理财产品最低投资份额");
        }
//        if (investmentNum.compareTo(financial.getMaxInvestment()) == 1) {
//            return new Resp(Resp.PARAM_ERROR, "投资金额大于理财产品最高投资份额");
//        }
        if (investmentNum.compareTo(financial.getLeftPortion()) == 1) {
            return new Resp(Resp.PARAM_ERROR, "投资金额超过理财产品剩余份额");
        }
        //判定该理财产品投资方式所需币种
        switch (financial.getInvestmentType()) { //投资类型 10:BTC,20:ETH,100:BHPC
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(currentUser().getUserId());
                if (null == bitCoinWallet) {
                    return new Resp(Resp.PARAM_ERROR, "余额不足");
                }
                //钱包可用余额跟理财产品投资额进行比较
                if (bitCoinWallet.getAvailBalance().compareTo(investmentNum) == -1) {
                    return new Resp(Resp.PARAM_ERROR, "投资金额大于钱包可用余额");
                }
                //扣除钱包对应总额跟可用余额
                bitCoinWallet.setBalance(bitCoinWallet.getBalance().subtract(investmentNum));
                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(investmentNum));
                bitcoinWalletRepository.save(bitCoinWallet);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(currentUser().getUserId());
                if (null == ethereumWallet) {
                    return new Resp(Resp.PARAM_ERROR, "余额不足");
                }
                //钱包可用余额跟理财产品投资额进行比较
                if (ethereumWallet.getAvailBalance().compareTo(investmentNum) == -1) {
                    return new Resp(Resp.PARAM_ERROR, "投资金额大于钱包可用余额");
                }
                //扣除钱包对应总额跟可用余额
                ethereumWallet.setBalance(ethereumWallet.getBalance().subtract(investmentNum));
                ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().subtract(investmentNum));
                ethereumWalletRepository.save(ethereumWallet);
                break;
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(currentUser().getUserId());
                if (null == bhpcWallet) {
                    return new Resp(Resp.PARAM_ERROR, "余额不足");
                }
                //钱包可用余额跟理财产品投资额进行比较
                if (bhpcWallet.getAvailBalance().compareTo(investmentNum) == -1) {
                    return new Resp(Resp.PARAM_ERROR, "投资金额大于钱包可用余额");
                }
                //扣除钱包对应总额跟可用余额
                bhpcWallet.setBalance(bhpcWallet.getBalance().subtract(investmentNum));
                bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().subtract(investmentNum));
                bhpcWalletRepository.save(bhpcWallet);
                break;
            default:
                break;
        }
        //理财产品的可用余额减少
        financial.setLeftPortion(financial.getLeftPortion().subtract(investmentNum));
        financialRepository.save(financial);
        //创建用户理财明细表
        UserFinancialRecord userFinancialRecord = new UserFinancialRecord();
        userFinancialRecord.setFinancialId(financialId);
        userFinancialRecord.setUserId(currentUser().getUserId());
        userFinancialRecord.setInvestmentPortion(investmentNum);
        userFinancialRecord.setDivide(new BigDecimal(BigInteger.ZERO));
        userFinancialRecord.setInvestmentDate(new Date());
        userFinancialRecord.setEmail(currentUser().getEmail());
        userFinancialRecord.setIsRelease(1);//未释放
        userFinancialRecordRepository.save(userFinancialRecord);
        //用户理财表
        UserEarnings userEarnings = userEarningsRepository.findByUserId(currentUser().getUserId());
        if (ParamUtil.isBlack(userEarnings)) {
            userEarnings = new UserEarnings();
        }
        userEarnings.setUserId(currentUser().getUserId());
        userEarnings.setBhpcEarnings(new BigDecimal(BigInteger.ZERO));
        userEarnings.setBtcEarnings(new BigDecimal(BigInteger.ZERO));
        userEarnings.setEthEarnings(new BigDecimal(BigInteger.ZERO));
        userEarnings.setEmail(currentUser().getEmail());
        userEarningsRepository.save(userEarnings);
        return new Resp("购买成功");
    }
}
